/**
 * Created by vincent on 2017/9/20.
 */
moduleStudyMap.controller("SetH5Controller", function ($$CambioConfig, $scope, $log, $state, LoadingService, DialogService,
                                                       EnumService, ConstantService, StudyClassResource, DynamicHtmlResource) {
    $scope.$on("ngDialog.opened", function () {
        dialogOnLoad();
        getH5StudyClass();
    });

    var getH5StudyClass = function () {
        if (StringUtils.isNlOrUndOrEmpty($scope.currentStudyClass.targetId)) {
            $scope.setH5StudyClass = {
                dynamicHtml: {}
            };
        } else {
            //LoadingService.show();
            StudyClassResource.getTarget({studyClassId: $scope.currentStudyClass.id}, function (h5StudyClassResult) {
                $log.debug("课程相关H5信息", h5StudyClassResult);
                $scope.setH5StudyClass = h5StudyClassResult;
                //LoadingService.hide();
            }, function () {
                //LoadingService.hide();
            });
        }
    };

    $scope.queryDynamicHtml = function () {
        //LoadingService.show();
        DynamicHtmlResource.findByKey({}, {key: $scope.htmlKey}, function (htmlResult) {
            $log.debug("H5项目匹配结果", htmlResult);
            $scope.htmlResult = htmlResult;
            //LoadingService.hide();
        });
    };

    $scope.setCurrentHtml = function (html) {
        $scope.setH5StudyClass.dynamicHtml = {
            id: html.dynamicHtmlId,
            subject: html.subject,
            projectName: html.projectName,
            code: html.code
        };
    };

    $scope.cancel = function () {
        DialogService.close();
    };

    $scope.submit = function () {
        if ($scope.setHSCForm.$valid) {
            if (!StringUtils.isNlOrUndOrEmpty($scope.setH5StudyClass.dynamicHtml.id)) {
                //LoadingService.show();
                StudyClassResource.setH5({studyClassId: $scope.currentStudyClass.id}, $scope.setH5StudyClass, function (studyClassResult) {
                    $log.debug("设置课程H5结果", studyClassResult);
                    angular.copy(studyClassResult, $scope.currentStudyClass);
                    DialogService.close();
                    //LoadingService.hide();
                    Alert.success("设置H5成功！");
                }, function () {
                    //LoadingService.hide();
                });
            } else {
                Alert.error("课程的H5项目信息不可为空！");
            }
        }
    };

});